package offer;

public class Solution44 {
    public int findNthDigit(int n) {
        if (n < 10) {
            return n;
        }
        int[] a = {10, 90, 900, 9000, 90000, 900000, 9000000, 90000000, 900000000};
        long sum = 0;
        int i = 0;
        while (sum <= n) {
            i++;
            sum += (long) a[i - 1] * (i);
        }
        sum -= (long) a[i - 1] * (i);
        int m = 1;
        for (int j = 0; j < i - 1; j++) {
            m *= 10;
        }
        m += (n - (int) sum) / i;
        int nth = (n - (int) sum) % i;
        return Integer.toString(m).charAt(nth) - '0';
    }
}
